package com.md.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

public class FileUtil {
	public static void dealBackExcel() {
		String localPath = "C:\\Users\\huacai\\Desktop\\T0back\\";
		File file = new File(localPath);
		File[] tempList = file.listFiles();
		int filenumber = tempList.length;
		if (filenumber > 0) {
			for (int i = 0; i < filenumber; i++) {
				if (tempList[i].isFile()) {
					String fileNmae = tempList[i] + "";
					System.out.println("处理  " + fileNmae  +"开始 .....");
					//BackTServer.MainServer(fileNmae);
					int beginIndex = fileNmae.length()-19;
					String dealPath = localPath + "deal\\ "+ fileNmae.substring(beginIndex);
					DealFileCopy(fileNmae,dealPath);
					tempList[i].delete();
//					System.out.println("处理  " + fileNmae  +"结束 .....");
				}else{
					System.out.println("******不是文件*******");
				}
			}
		}else{
			System.out.println("路径错误");
		}
	}
	private static void DealFileCopy(String source, String target) {  
		File sourceFile = new File(source);
		File targetFile = new File(target);
	    FileChannel in = null;  
	    FileChannel out = null;  
	    FileInputStream inStream = null;  
	    FileOutputStream outStream = null;  
	    try {  
	        inStream = new FileInputStream(sourceFile);  
	        outStream = new FileOutputStream(targetFile);  
	        in = inStream.getChannel();  
	        out = outStream.getChannel();  
	        in.transferTo(0, in.size(), out);  
	    } catch (IOException e) {  
	        e.printStackTrace();  
	    } finally {  
	    	try {
				inStream.close();
				in.close();
				outStream.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	    }  
	} 

	/**
	 * 上传文件
	 * @param file
	 * @param filePath
	 * @return
	 */
	public static String uploadCert(MultipartFile file,String filePath){
		String fileName="";
		String newFileName="";
		try {
			if (!file.isEmpty()) {
				fileName = file.getOriginalFilename();
				//获取文件后缀
				String suffix = fileName.substring(fileName.lastIndexOf("."));
				//生成新的文件名
				newFileName = UUID.randomUUID().toString()+suffix;
				File tempFile = new File(filePath,newFileName);
				if(!tempFile.exists()){
					tempFile.mkdirs();
				}
                file.transferTo(tempFile);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return newFileName;	
	}
	
	
	
	/**
	 * 上传文件(重命名文件)
	 * @param file
	 * @return 文件名
	 * @throws Exception
	 */
	public static String upload(MultipartFile file,String savePath) throws Exception{
		File saveFile = new File(savePath);

		try {
			if (!saveFile.exists()) {
				saveFile.mkdirs();
			}
		} catch (Exception e) {
			//LogWriter.error("[创建文件夹异常]");
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");
		// 新文件名 当前时间的年月日小时分钟秒+随即三位数
		String filePath = savePath;
		String ymd = sdf.format(new Date());
		String newName = DigestUtils.md5Hex(ymd + new Random().nextInt(30000000));
		String fileName = file.getOriginalFilename();
		newName = newName+fileName.substring(fileName.lastIndexOf("."));
		filePath=filePath+newName;
		CreateFile(filePath);
		File localFile = new File(filePath);
		file.transferTo(localFile);
		return newName;
	} 
	
	public static boolean CreateFile(String destFileName) {
		File file = new File(destFileName);
		if (file.exists()) {
			// System.out.println("创建单个文件" + destFileName + "失败，目标文件已存在！");
			return false;
		}
		if (destFileName.endsWith(File.separator)) {
			System.out.println("创建单个文件" + destFileName + "失败，目标不能是目录！");
			return false;
		}
		if (!file.getParentFile().exists()) {
			System.out.println("目标文件所在路径不存在，准备创建。。。");
			if (!file.getParentFile().mkdirs()) {
				System.out.println("创建目录文件所在的目录失败！");
				return false;
			}
		}
		// 创建目标文件
		try {
			if (file.createNewFile()) {
				System.out.println("创建单个文件" + destFileName + "成功！");
				return true;
			} else {
				System.out.println("创建单个文件" + destFileName + "失败！");
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("创建单个文件" + destFileName + "失败！");
			return false;
		}
	}
}
